package com.nf.health.app.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.http.Apis;
import com.nf.health.app.utils.JsonUtils;
import com.nf.health.app.utils.Util;
import com.nf.health.app.utils.ViewUtil;

/** 
* @ClassName: ForgetPasswordsActivity 
* @Description: 忘记密码页面
* @author Ian 
* @date 2015-11-4 上午9:53:06 
*  
*/
public class ForgetPasswordsActivity extends BaseActivity implements
		OnClickListener {
	private Button iv_rigister, btn_submit;
	private EditText et_teleNum, et_verification_code;
	private String verifi_code, curNum;
	private TimeCount timeCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContent(R.layout.activity_forgrt_passwords);
		setTitleBar(R.layout.titlebar_base);
		
		ViewUtil.setText(this, R.id.titlebar_title_tv, "忘记密码");
		et_teleNum = (EditText) findViewById(R.id.et_teleNum);
		et_verification_code = (EditText) findViewById(R.id.et_verification);
		btn_submit = (Button) findViewById(R.id.iv_rigister_submit);
		iv_rigister = (Button) findViewById(R.id.iv_rigister);
		
		initListener();
		
		timeCount = new TimeCount(60000, 1000);
	}
	private void initListener() {
		
		iv_rigister.setOnClickListener(this);
		btn_submit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_rigister:
			curNum = et_teleNum.getText().toString();
			if (TextUtils.isEmpty(curNum)) {
				showToast("手机号码不能为空");
				break;
			}
			if (!Util.isPhone(curNum)) {
				showToast("手机号码输入错误！");
				break;
			}
			timeCount.start();
//			verifi_code = Code.getInstance().getCode();
//			if(LogUtil.IS_DEBUG){
//				et_verification_code.setText(verifi_code);
//			}
			provider.requestMSGCode(curNum, Constants.SMS_RESET,"verifycode");
			break;
		case R.id.iv_rigister_submit:
			curNum = et_teleNum.getText().toString();
			if (TextUtils.isEmpty(curNum)) {
				showToast("手机号码不能为空");
				break;
			}
			if (TextUtils.isEmpty(et_verification_code.getText().toString())) {
				showToast("验证码码不能为空");
				break;
			}
			if (!Util.isPhone(curNum)) {
				showToast("手机号码输入错误！");
				break;
			}
				provider.requestMSGCodeCode(curNum,et_verification_code.getText().toString(),Constants.SMS_RESET, "yanzheng");

			break;
		}
		super.onClick(v);
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName == "verifycode") {
			showToast("验证码发送成功!");
			if (Apis.HOST_COMMON.contains("test.")){
				String res = (String) object;
				String data = JsonUtils.getFiledData(res,"data");
				String code = JsonUtils.getFiledData(data,"verifycode");
				et_verification_code.setText(code);
			}
		}else if ("yanzheng".equals(actionName)) {//判断验证码与手机号是否相符
			String msg = object+"";
			if(msg.contains("验证成功")){
				if (!TextUtils.isEmpty(curNum) && curNum.length() == 11) {
						Intent newPaw = new Intent(this,ConfirmPwdActivity.class);
						newPaw.putExtra("curNum", curNum);
						newPaw.putExtra("verifi_code", et_verification_code.getText().toString());
						startActivity(newPaw);
				} else {
					showToast("手机号/短信验证为空");
				}
			}else{
				showToast("验证码错误");
			}
		}
	}
	//计时重发
		private class TimeCount extends CountDownTimer {
			/**
			 * @param millisInFuture 计时器执行的时长
			 * @param countDownInterval 执行一次的时间间隔
			 */
	        public TimeCount(long millisInFuture, long countDownInterval) {
	            super(millisInFuture, countDownInterval);
	        }
	 
	        @Override
	        public void onTick(long millisUntilFinished) {
	        	iv_rigister.setText("重发（"+millisUntilFinished / 1000+"秒）");
//	        	et_verification_code.setFocusable(true);
//	        	et_verification_code.requestFocus();
	        	iv_rigister.setClickable(false);
	        }
	 
	        @Override
	        public void onFinish() {
	        	resetCodeBtn();
	        }

	    }
		private void resetCodeBtn() {
			iv_rigister.setClickable(true);
			iv_rigister.setText("获取验证码");
		}
}
